/**
options = {el:"操作的dom",min_width:"最小宽度",max_width:"最大宽度",}
**/
function Flexible(options){
  const MIN_WIDTH = 80,MAX_WIDTH = 200;
  var _this = this;
  this.min_width = options.min_width?options.min_width:MIN_WIDTH;
  this.max_width = options.max_width?options.max_width:MAX_WIDTH;
  this.middle_width_callback = options.middle_width_callback;
  this.min_width_callback = options.min_width_callback;
  this.associate_el = options.associate;
  if(!options.el){
    throw "操作的el不能为空";
  }

  if(!options.sider_bar){
    throw "滑动条sider_bar不能为空";
  }
  this.sider_bar = options.sider_bar;
  this.el = options.el;
  this.location = getLocation(_this.el);
  var isMouseDown = false;
  function mouseup(event){
    isMouseDown = false;
    $(document.body).css("cursor","default");
    event.stopPropagation();
    if(_this.sider_bar[0].releaseCapture){
      _this.sider_bar[0].releaseCapture();
    }else if(window.releaseEvents){
      window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
    }

    $(document).off("mousemove");
    $(document).off("mouseup");
    return false;
  }

  function mousedown(event){
    _this.location = getLocation(_this.el);
    var jqEle = $(event.currentTarget);
    isMouseDown = true;
    _this.el.attr("initx",event.clientX - _this.location.left);
    if(_this.sider_bar[0].setCapture){
      _this.sider_bar[0].setCapture();
    }else if(window.captureEvent){
      window.captureEvent(Event.MOUSEMOVE|Event.MOUSEUP);
    }
    $(document).on("mousemove",mousemove);
    $(document).on("mouseup",mouseup);

  //  event.stopPropagation();
     //return false;
  }

  function mousemove(event){
    var x = event.clientX - _this.location.left;

    var width = _this.el.width();
    var range = {min_x:width - 3,max_x : width + 10};

    if(!isMouseDown){
      return false;
    }

    width = width+(x - _this.el.attr("initx"));
    var min_width = _this.min_width+_this.location.left,max_width = _this.max_width+_this.location.left;

    if(width > _this.min_width && width < _this.max_width){

      _this.el.width(width);
      _this.el.attr("initx",x);
      if(_this.middle_width_callback){
        _this.middle_width_callback();
      }
   }else if(width <= _this.min_width){
     _this.el.width( _this.min_width);
     _this.el.attr("initx",_this.min_width);
     if(_this.min_width_callback){
       _this.min_width_callback();
     }
   }else if(width >= _this.max_width){
     _this.el.width(_this.max_width);
     _this.el.attr("initx",_this.max_width);
   }
   if(_this.associate_el){
     var associate = _this.associate_el.innerEl;
     associate.el.css("left",(_this.location.left+_this.el.width()+parseInt(_this.el.css("border-right-width")))+"px");
     var width = _this.el.width();
    if(associate.callback){
      associate.callback(associate.el);
    }
     while(associate.innerEl){
       var location = getLocation(associate.el);
       var width = associate.el.width();
       associate = associate.innerEl;
        var associate_el = associate.el;
        associate_el.css("left",(location.left+width)+"px");
        if(associate.callback){
          associate.callback(associate.el);
        }
     }
   }
//event.stopPropagation();
    return false;
  }

 _this.sider_bar.mousemove(function(event){
     var jqEle = $(event.currentTarget);
   jqEle.css("cursor","ew-resize");
 });


  _this.sider_bar.mousedown(mousedown);
}
